﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hospital.Models
{
    public class SpravkaReposite
    {
        private HostleDataContext _datacontext;

        public SpravkaReposite(HostleDataContext datacontext)
        {
            _datacontext = datacontext;
        }

        public IEnumerable<Spravka> GetSpravki()
        {
            return _datacontext.Spravkas.OrderBy(s => s.Name);
        }

        public Spravka GetSpravka(int id)
        {
            return _datacontext.Spravkas.SingleOrDefault(s => s.Id_Bol == id);
        }

        public Spravka AddSpravka(string name, string simptom, string lechenie)
        {
            Spravka s = new Spravka { Name = name, Symptom = simptom, Lechenie = lechenie };
            _datacontext.Spravkas.InsertOnSubmit(s);
            _datacontext.SubmitChanges();
            return s;
        }

        public void UpdateSpravka(Spravka s)
        {
            Spravka dbSpr = GetSpravka(s.Id_Bol);
            dbSpr.Name = s.Name;
            dbSpr.Symptom = s.Symptom;
            dbSpr.Lechenie = s.Lechenie;
            _datacontext.SubmitChanges();
        }

        public void DeleteSpravka(int id)
        {
            Spravka dbSpr = GetSpravka(id);
            _datacontext.Spravkas.DeleteOnSubmit(dbSpr);
            _datacontext.SubmitChanges();
        }
    }
}